/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.utils;

import org.apache.commons.lang.StringUtils;

public class StringUtils2 {

    /**
     * Removes the some chars starting from index start from the given string.
     *
     * @param str   the string
     * @param start the start index
     * @param end   the offset after the last character to delete
     * @return the resulting string
     */
    public static String remove(String str, int start, int end) {
        if (StringUtils.isEmpty(str) || end <= start || start >= str.length()) return str;
        if (end >= str.length()) return str.substring(0, start);
        return str.substring(0, start) + str.substring(end);
    }

    /**
     * Replaces the some chars starting from index start from the given string with another string.
     *
     * @param str        the string
     * @param start      the start index
     * @param end        the offset after the last character to delete
     * @param replaceStr the string to replace with
     * @return the resulting string
     */
    public static String replace(String str, int start, int end, String replaceStr) {
        if (StringUtils.isEmpty(replaceStr)) return remove(str, start, end);
        if (StringUtils.isEmpty(str) || end <= start || start >= str.length()) return str;
        if (end >= str.length()) return str.substring(0, start) + replaceStr;
        return new StringBuilder(str.length() + replaceStr.length() + start - end).
                append(str.substring(0, start)).
                append(replaceStr).
                append(str.substring(end)).
                toString();
    }

    /**
     * Efficiently concatenates the given strings.
     * @param strings the string to join
     * @return the concatenated string
     */
    public static String join(String... strings) {
        int totalLen = 0;
        for (String string : strings) {
            if (string != null) totalLen += string.length();
        }
        StringBuilder sb = new StringBuilder(totalLen);
        for (String string : strings) {
            sb.append(string);
        }
        return sb.toString();
    }

    /**
     * Efficiently concatenates the given strings using the given separator.
     * @param separator the separator string
     * @param strings the string to join
     * @return the concatenated string
     */
    public static String join(char separator, String... strings) {
        int totalLen = 0;
        for (String string : strings) {
            if (totalLen > 0) totalLen++;
            if (string != null) totalLen += string.length();
        }
        StringBuilder sb = new StringBuilder(totalLen);
        for (String string : strings) {
            if (sb.length() > 0) sb.append(separator);
            sb.append(string);
        }
        return sb.toString();
    }
}